package com.leo.foodmaster.commons.util;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.leo.foodmaster.admin.system.entity.SysDictData;
import com.leo.foodmaster.commons.constants.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;

/**
 * <p>
 * 字典工具类
 * </p>
 *
 * @author ：Leo
 * @since ：2021/7/25 12:47
 */
public class DictUtils {
    /**
     * 根据字典类型和字典值获取字典标签
     *
     * @param dictType 字典类型
     * @param dictValue 字典值
     * @param separator 分隔符
     * @return 字典标签
     */
    public static String getDictLabel(String dictType, String dictValue, String separator) {
        StringBuilder propertyString = new StringBuilder();
        RedisTemplate<String, Object> redisTemplate = SpringUtils.getBean("stringRedisTemplate");
        String dictKey = Constants.SYS_DICT_KEY + dictType;
        String jsonStr = (String) redisTemplate.opsForValue().get(dictKey);
        List<SysDictData> dictDataList = JSONUtil.toList(JSONUtil.parseArray(jsonStr), SysDictData.class);

        if (StringUtils.containsAny(separator, dictValue) && !dictDataList.isEmpty()) {
            for (SysDictData dict : dictDataList) {
                for (String value : dictValue.split(separator)) {
                    if (value.equals(dict.getDictValue())) {
                        propertyString.append(dict.getName()).append(separator);
                        break;
                    }
                }
            }
        } else {
            for (SysDictData dict : dictDataList) {
                if (dictValue.equals(dict.getDictValue())) {
                    return dict.getName();
                }
            }
        }
        return StringUtils.stripEnd(propertyString.toString(), separator);
    }

    /**
     * 根据字典类型和字典标签获取字典值
     *
     * @param dictType 字典类型
     * @param dictLabel 字典标签
     * @param separator 分隔符
     * @return 字典值
     */
    public static String getDictValue(String dictType, String dictLabel, String separator) {
        StringBuilder propertyString = new StringBuilder();
        RedisTemplate<String, Object> redisTemplate = SpringUtils.getBean(RedisTemplate.class);

        String jsonStr = (String) redisTemplate.opsForValue().get(dictType);
        List<SysDictData> datas = JSONUtil.parseArray(jsonStr).toList(SysDictData.class);

        if (StringUtils.containsAny(separator, dictLabel) && !datas.isEmpty()) {
            for (SysDictData dict : datas) {
                for (String label : dictLabel.split(separator)) {
                    if (label.equals(dict.getName())) {
                        propertyString.append(dict.getDictValue()).append(separator);
                        break;
                    }
                }
            }
        } else {
            for (SysDictData dict : datas) {
                if (dictLabel.equals(dict.getName())) {
                    return dict.getDictValue();
                }
            }
        }
        return StringUtils.stripEnd(propertyString.toString(), separator);
    }
}
